import router from "./router";
import store from "./store";
import { Dialog, Toast } from "vant";
import { getToken } from "@/utils/auth"; // get token from cookie
import { pathMatcher } from "@/utils/index";
import { getUserInfo } from "./utils/localStorage";

const whiteList = [
  "/login",
  "/registry",
  "/forget",
  "/argeement",
  "/",
  "/notice/list/**",
  "/index",
  "/category",
  "/detail/**",
  "/diamondGoodsList/**",
  "/search/**",
]; // 白名单

router.beforeEach(async (to, from, next) => {
  const version = require("../package.json").version;
  const versionLocal = localStorage.getItem("version");
  console.log('to',to);
  console.log('from',from);
  if(to.name=='Home'){
    if((from.name =='Detail' || from.name =='Diamand') || from.path == '/'){
      console.log(from.path)
      to.meta.keepAlive = true
    }
  }
  
  // if(to.)
  const now = sessionStorage.getItem("from");
  if (to.query.name == "Home") {
    if (to.query?.from) {
      sessionStorage.setItem("from", to.query.from);
      if (to.query?.from != now) {
        location.reload();
      }
    } else {
      // sessionStorage.setItem('from','')
      if ("" != now) {
        location.reload();
      }
    }
  }

  console.log("线上版本：", version);
  console.log("当前版本：", versionLocal);
  const h = new Date().getHours();
  const m = new Date().getMinutes();
  // if(h == 23&& m == 59){
  // localStorage.removeItem('fresh')
  // Dialog.confirm({
  //   title:'系统温馨提示',
  //   width:500,
  //   message:'系统维护中，请刷新...',

  // }).then(res=>{
  //   location.reload()
  // })
  // }
  // alert(version)
  if (
    versionLocal != version ||
    versionLocal == undefined ||
    versionLocal == null
  ) {
    console.log("当前版本不正确");
    //   Toast({message:'当前版本不正确，正在刷新',duration:2000})
    //   // localStorage.clear()
    //   localStorage.setItem('version',version)
    //   setTimeout(()=>{
    //     // sessionStorage.clear()
    sessionStorage.setItem('needRefresh',true)
    window.location.reload(true);
    //   },2000)
    //   return false
  }
  localStorage.setItem("version", version);

  // 设置标题
  document.title = to.meta.title || "门店需要改";

  // 根据token判断用户是否登录
  const hasToken = getToken();
  console.log(hasToken);
  if (to.path == "/login") {
    if (hasToken) {
      next("/");
    }
  }
  // debugger
  if (hasToken != null) {
    next();
  } else {
    if (pathMatcher(whiteList, to.path)) {
      next()
      // window.location.href = window.location.hostname+':'+location.port+'/#'+to.path
    } else {
      if (getUserInfo().mobile != null || getUserInfo().password != null) {
        store.dispatch("user/login", getUserInfo());
        next();
      } else {
        //反之则去登录页面;
        next(`/login?redirect=${encodeURIComponent(location.href)}`);
      }
    }
  }
  // if (to.path === '/login') {
  //   next({ path: '/' })
  // } else {
  //   const hasUserInfo = store.getters.id
  //   if (hasUserInfo) {
  //     next()
  //   } else {
  //     try {
  //       await store.dispatch('user/getInfo')
  //       next()
  //     } catch (error) {
  // if (pathMatcher(whiteList, to.path)) {
  //   next()
  // } else {
  // }
  //       }
  //     }
  //   }
  // } else {
  //   if (pathMatcher(whiteList, to.path)) {
  //     next()
  //   } else {

  //     next(`/login?redirect=${encodeURIComponent(location.href)}`)
  //   }
  // }
});

router.afterEach(() => {
  Toast.clear();
});
